package com.bdqn.controller;


import com.bdqn.entity.Article;
import com.bdqn.entity.ArticleWithBLOBs;
import com.bdqn.service.ArticleService;
import com.bdqn.service.ArticleTypeService;
import com.bdqn.util.Page;
import com.bdqn.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleTypeService articleTypeService;

    //分页查询检索文章
    @GetMapping("/flowArticle")
    @ResponseBody
    public Page<Article> flowArticle(@RequestParam(required = false) Integer tid,
                                     @RequestParam(defaultValue = "1") Integer pageIndex,
                                     @RequestParam(defaultValue = "8") Integer pageSize,
                                     Model model) {
        Page<Article> articlePage = articleService.getPageArticles(tid, pageIndex, pageSize).getData();
        return articleService.getPageArticles(tid, pageIndex, pageSize).getData();
    }

    //分页查询检索文章
    @GetMapping("/{isDraft}-listArticle")
    @ResponseBody
    public Page<Article> listArticle(String timeRange, String title,Integer type,
                                     @RequestParam(defaultValue = "1") Integer pageIndex,
                                     @RequestParam(defaultValue = "5") Integer pageSize,
                                     @PathVariable("isDraft") String isDraft,
                                     HttpSession session) {
        return articleService.getPageArticles1(timeRange, title,type, pageIndex, pageSize, session,Boolean.valueOf(isDraft)).getData();
    }



    /**
     * 富文本图片上传
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/articleUploadImg")
    @ResponseBody
    public Map<String, Object> articleUploadImg(@RequestParam(name = "file", required = false) CommonsMultipartFile file,
                                                HttpServletRequest request) {
        return articleService.uploadArticleImg(request, file);
    }


    /**
     * 增删改
     *
     * @param flag
     * @param article
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/{flag}-article")
    @ResponseBody
    public Result editArticle(
        @PathVariable("flag") String flag, ArticleWithBLOBs article,
        @RequestParam(name = "upload", required = false)
            CommonsMultipartFile file, HttpServletRequest request) {
        return articleService.editArticle(flag, article, file, request);
    }

    //查看
    @GetMapping("/{flag}-find")
    public String findArticle(@PathVariable("flag") String flag,String uuid,Model model){
        model.addAttribute("article",articleService.getArticleByld(uuid));
        model.addAttribute("flag",flag);
        //推荐博客的类型
        model.addAttribute("articleTypes",articleTypeService.getAllArticleType());
        return  "client/user-editblog";
    }




}
